package com.malty.mall.controller.ums;

import com.malty.mall.common.CommonResult;
import com.malty.mall.domain.dto.req.UmsAdminLoginParam;
import com.malty.mall.domain.dto.req.UmsAdminParam;
import com.malty.mall.domain.entity.ums.UserAdmin;
import com.malty.mall.service.ums.IUserAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author malty
 */
@Api(tags = "管理员")
@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class UserController {
    private final IUserAdminService userAdminService;

    @ApiOperation("注册")
    @PostMapping("/register")
    public CommonResult<UserAdmin> register(@Validated @RequestBody UmsAdminParam umsAdminParam) {
        return CommonResult.success(userAdminService.register(umsAdminParam));
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public CommonResult<String> login(@Validated @RequestBody UmsAdminLoginParam umsAdminLoginParam) {
        return CommonResult.success(userAdminService.login(umsAdminLoginParam.getUsername(), umsAdminLoginParam.getPassword()));
    }

    @ApiOperation("登录")
    @GetMapping("/refresh_token")
    public CommonResult<String> refreshToken(@RequestHeader(name = "Authorization") String oldToken) {
        return CommonResult.success(userAdminService.refreshToken(oldToken));
    }

    @ApiOperation("关联角色")
    @PostMapping("/role_relate")
    public CommonResult<Boolean> login(Integer uid, Integer roleId) {
        return CommonResult.success(userAdminService.relateRole(uid, roleId));
    }

    @GetMapping("/test")
    public CommonResult<String> test() {
        return CommonResult.success("test");
    }
}
